package cn.leonis.leetcode;

/**
 * @Author Tobi
 * @Date 2021-07-14 下午12:40
 * 在旋转数组里面找出最小值
 */
public class FindMinimumInRotatedSortedArraySolution {

    public static void main(String[] args) {
        int[] nums = new int[]{2,3,4,5,1};
        FindMinimumInRotatedSortedArraySolution solution = new FindMinimumInRotatedSortedArraySolution();
        System.out.println(solution.findMin(nums));
    }

    /**
     * 通过二分查找找出每个边界，因为必须是递增的数组
     * @param nums
     * @return
     */
    public int findMin(int[] nums) {
        int min = nums[0];
        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if (nums[left] <= nums[mid]) {
                min = Math.min(min, nums[left]);
                left = mid + 1;
            }else {
                min = Math.min(min, nums[mid]);
                right = mid - 1;
            }
        }

        return min;
    }
}
